<?php 
namespace Admin\Controller;
use Think\Controller;
class PublicController  extends Controller{
	
	public function bangding_next(){
		$this->mobile = I('mobile');
		$this->display();
	}
	/**
	 * 登录页面
	 */
	public function login(){
		$this->display();
	}
	
	public function test(){
		$m = D('Admins');
		$map['username'] = 'admin';
		$result = $m->where($map)->relation(true)->find();
		dump($m->_sql());
		dump($result);
	}
	
	/**
	 * 执行登录
	 */
	public function login_post(){
		$map['username|email|mobile'] = $_POST['username'];
		$map['password'] = md5($_POST['password']);
		$map['role'] = array(array('like','%|ROLE_ADMIN|%'),array('like','%|ROLE_SUPER_ADMIN|%'),'or');
		$m = D('Users');
		$result = $m->where($map)->relation(true)->find();
		if($result){
			if(!$result['status']){
				$this->error('该用户已被禁用');
			}
			$user['uid'] = $result['uid'];
			$user['username'] = $result['username'];
			$user['last_ip'] = $result['admin_last_ip'];
			$user['last_time'] = $result['admin_last_time'];
			$user['login_count'] = $result['admin_login_count'];
			
			$rmap['uid'] = $result['uid'];
			$rmap['group'] = 'admin';
			$rules = D('AuthGroup')->find($result['group_id']);
			$user['str_rules'] = $rules['rules'];
			$user['rules'] = explode(',', $rules['rules']);
			$user['role'] = $rules['title'];
			$_SESSION['loginAdmin'] = $user;
			
			
			$result['admin_last_ip']=get_client_ip();
			$result['admin_last_time']=date("Y-m-d H:i:s");
			$result['admin_login_count'] += 1;
			unset($result['group_id']);
			$m->save($result);
			setcookie("admin_username",$result['username'],time()+30*24*3600,"/");
			$this->success("登录验证成功！",U("Index/index"));
			
		}else{
			$this->error('用户名或账号不正确');
		}
		$this->display();
	}
	
	public function logout(){
		session('[destroy]'); 
    	$this->redirect("public/login");
	}
	
	
}
?>